home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / PennyWise™ Framework / PennyView / TEMPLATE_DIALOG.c < prev    next >
Encoding:
Text File  |  1994-08-11  |  15.7 KB  |  468 lines  |  [TEXT/KAHL]

  1. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. //                                                                                //
  3. //                                                                                //
  4. //                    Copyright PennyWise Software, 1994.                            //
  5. //                                                                                //
  6. //            Part of the PennyWise Software Application Framework                //
  7. //                                                                                //
  8. //                                                                                //
  9. //            TEMPLATE_DIALOG.c            Written by Peter Kaplan                    //
  10. //                                                                                //
  11. //                                                                                //
  12. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  13. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  14. //
  15. //    This is a template for a PennyWise Software Application Framework window
  16. //
  17. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  18. #include "PWFramework.h"
  19. #include "PWWindowList.h"
  20. #include "WindowID.h"
  21. #include "TEMPLATE_DIALOG.h"
  22. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  23. //        Directions for customizing this templete to work
  24. //        with your own window types
  25. //
  26. //        1 —    Rename this file according to the function of the window (Unique Name).
  27. //            Ex. If it is a window for text call the file TextWindow.c
  28. //        2 - Reame the header file according to the function of the window.
  29. //        3 - Do a search and replace in this file and the header file.
  30. //            Search for the word TEMPLATE_DIALOG and replace it with the new 
  31. //            name of the file from step 1 & 2. 
  32. //        4 — Add this file to the project (Source Menu)
  33. //        5 - Open "WindowID.h" and add kWINDOW_ID_TEMPLATE_DIALOG value to 
  34. //            the list. Just take the next number available
  35. //        6 - Also in "WindowID.h" increment kMAX_WINDOW_IDS by one.
  36. //        7 — Add functionality as needed
  37. //        8 - Add the header file to InitApplication.c file and
  38. //            call Init[xxx]Handlers from InitAppliaction. [xxx] is the name from 1,2 & 3
  39. //        9 -    Add Open calls where needed.
  40. //
  41. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  42. // These procedures are static. They will only be called by name from here
  43. // outside refrences will use our window proc list.
  44. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow);
  45. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow);
  46. static    void    ThisWindowZoomIn    (EventRecord* theEvent, WindowPtr theWindow);
  47. static    void    ThisWindowZoomOut    (EventRecord* theEvent, WindowPtr theWindow);
  48. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow);
  49. static    void    ThisWindowClick        (EventRecord* theEvent, WindowPtr theWindow);
  50. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow);
  51. static    void    ThisWindowActivate    (EventRecord* theEvent, WindowPtr theWindow);
  52. static    void    ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow);
  53. static    void    ThisWindowDrag        (EventRecord* theEvent, WindowPtr theWindow);
  54. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow);
  55. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow);
  56. static    void    ThisWindowKeyDown    (EventRecord* theEvent, WindowPtr theWindow);
  57. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow);
  58. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow);
  59. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID);
  60. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect);
  61. static    void    ThisWindowBackground(EventRecord* theEvent, WindowPtr theWindow);
  62. static    void    ThisWindowGetScrap    (EventRecord* theEvent, WindowPtr theWindow);
  63. static    void    ThisWindowPutScrap    (EventRecord* theEvent, WindowPtr theWindow);
  64. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  65. // This record holds all the information about this window
  66. // You can add fields to this record.
  67. // NOTE:ALL WINDOW RECORDS MUST START WITH THIS
  68. //        HEADER OR ELSE THE FRAMEWORK WILL NOT 
  69. //        FUNCTION PROPERLY. YOU'VE BEEN WARNED!
  70. typedef struct    OurWinRecord {
  71.     WindowParamHeader    theHeader;
  72.     short                isDirty;
  73.     }OurWinRecord, *OurWinPtr, **OurWinHandle;
  74. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  75. // Macros for accessing the header data
  76. // you can add your own as you add fields
  77. // to the OurWinRecord. 
  78. // NOTE:THESE MACROS ASSUME theData HOLDS
  79. //        A VALID COPY OF OurWinHandle. 
  80. #define THE_ID        (*theData)->theHeader.theID
  81. #define IS_DIRTY    (*theData)->isDirty
  82. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  83. void InitTEMPLATE_DIALOGHandlers()
  84. {
  85.     PWInstallWindowType    (kWINDOW_ID_TEMPLATE_DIALOG, kWINDOW_TYPE_DIALOG);
  86.     PWInstallCreate        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowCreate);
  87.     PWInstallDispose    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowDispose);
  88.     PWInstallZoomIn        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowZoomIn);
  89.     PWInstallZoomOut    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowZoomOut);
  90.     PWInstallResize        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowResize);
  91.     PWInstallClick        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowClick);
  92.     PWInstallUpdate        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowUpdate);
  93.     PWInstallActivate    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowActivate);
  94.     PWInstallDeactivate    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowDeactivate);
  95.     PWInstallIdle        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowIdle);
  96.     PWInstallCursor        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowCursor);
  97.     PWInstallKeyDown    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowKeyDown);
  98.     PWInstallDrag        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowDrag);
  99.     PWInstallPreMenu    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowPreMenu);
  100.     PWInstallMenu        (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowDoMenu);
  101.     PWInstallPostMenu    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowPostMenu);
  102.     PWInstallGrowRect    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowGrowRect);
  103.     PWInstallBackground    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowBackground);
  104.     PWInstallScrap2Appl    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowGetScrap);
  105.     PWInstallAppl2Scrap    (kWINDOW_ID_TEMPLATE_DIALOG, ThisWindowPutScrap);
  106. }
  107. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  108. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow)
  109. {
  110. // This routine will very rarely contain anything worthwhile
  111. // You will make custom routines for most windows that will be exported
  112. // via the include file
  113. }
  114. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  115. //    Since we will be exporting this we don't have to conform to any particular input format
  116. void    TEMPLATE_DIALOGOpen(void)
  117. {
  118. WindowPtr        theWindow;
  119. OurWinHandle    theData;
  120. Rect            windowBounds;
  121.  
  122.     SetRect(&windowBounds, 40, 40, 200,200);
  123.     theWindow = NewDialog(NULL,&windowBounds,"\p",FALSE,dBoxProc,(WindowPtr)-1,FALSE,0,NULL);
  124.     if (theWindow) {
  125.         // We sucessfully got the window
  126.         
  127.         // Now we have to allocate our storage
  128.                 theData = (OurWinHandle) NewHandle(sizeof(OurWinRecord));
  129.         if (theData) {
  130.             // We sucessfully allocated storage
  131.             
  132.             // So Lets set the port to our new window
  133.             SetPort(theWindow);
  134.             
  135.             // Here we would do any screen/size manipulations
  136.             // before we show the window
  137.             
  138.             
  139.             // Here we would allocate any other 
  140.             // objects that we need. (Controls, etc.)
  141.         
  142.             // Now lets set the id
  143.             THE_ID        = kWINDOW_ID_TEMPLATE_DIALOG;
  144.             IS_DIRTY    = FALSE;
  145.             
  146.             // Install our routines
  147.             SetWRefCon(theWindow, (long) theData);
  148.                     
  149.             // Show it & select it before we leave
  150.             ShowWindow(theWindow);
  151.             SelectWindow(theWindow);
  152.             }
  153.         else {
  154.             // We could not allocate memory for our window's data
  155.             // So lets get rid of the data
  156.             DisposeWindow(theWindow);
  157.             theWindow = NULL;
  158.             }
  159.         }
  160.     
  161.     if (!theWindow) {
  162.         SysBeep(1);
  163.         // We did not create the window
  164.         // You will probably want to put up a dialog here to explain why
  165.         }
  166. }
  167. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  168. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow)
  169. {
  170. OurWinHandle    theData;
  171. short            theResults;
  172.  
  173.     theData = (OurWinHandle) GetWRefCon(theWindow);
  174.     
  175.     // Want to save it
  176.     theResults = TRUE;
  177.     
  178.     if (IS_DIRTY) {
  179.         // Data has been changed we would probably want to put up 
  180.         // a “Save Changes to x” dialog here 
  181.         // But I'm just going to reset IS_DIRTY
  182.         IS_DIRTY = FALSE;
  183.         }
  184.     
  185.     // Do whatever saving you need to do here
  186.     
  187.     // Now lets break it down
  188.     HideWindow(theWindow);
  189.     DisposeHandle((Handle)theData);
  190.     DisposeDialog(theWindow);
  191.     
  192. return theResults;    // True if we closed it, false if we did not [ex. pressed cancel in save dialog]    
  193. }
  194. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  195. static    void    ThisWindowZoomIn    (EventRecord* theEvent, WindowPtr theWindow)
  196. {    // The defaults will do the right thing
  197. }
  198. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  199. static    void    ThisWindowZoomOut    (EventRecord* theEvent, WindowPtr theWindow)
  200. {    // The defaults will do the right thing
  201. }
  202. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  203. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow)
  204. {
  205. OurWinHandle    theData;
  206. GrafPtr            oldPort;
  207.  
  208.  
  209.     GetPort(&oldPort);
  210.     SetPort(theWindow);
  211.  
  212.     theData = (OurWinHandle) GetWRefCon(theWindow);
  213.     
  214.     EraseRect(&theWindow->portRect);
  215.  
  216.     // Do any control moving here
  217.     //•••••••••••••••••••••••••••
  218.         
  219.     InvalRect(&theWindow->portRect);
  220.  
  221.     SetPort(oldPort);
  222. }
  223. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  224. static    void    ThisWindowClick        (EventRecord* theEvent, WindowPtr theWindow)
  225. {
  226. OurWinHandle    theData;
  227. GrafPtr            oldPort;
  228. DialogPtr        theDialog;
  229. short            itemHit;
  230.  
  231.  
  232.     GetPort(&oldPort);
  233.     SetPort(theWindow);
  234.  
  235.     theData = (OurWinHandle) GetWRefCon(theWindow);
  236.     
  237.     // Test for hits here
  238.     //•••••••••••••••••••••••••••
  239.     if( DialogSelect( theEvent, &theDialog, &itemHit)) {
  240.         switch (itemHit) {
  241.             default:
  242.                 break;
  243.             }
  244.         }    
  245.     SetPort(oldPort);
  246. }
  247. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  248. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow)
  249. {
  250. OurWinHandle    theData;
  251. GrafPtr            oldPort;
  252.  
  253.  
  254.     GetPort(&oldPort);
  255.     SetPort(theWindow);
  256.  
  257.     theData = (OurWinHandle) GetWRefCon(theWindow);
  258.     
  259.     UpdtDialog( theWindow, theWindow->visRgn);
  260.  
  261.     // Draw the window here
  262.     //•••••••••••••••••••••••••••
  263.     
  264.     SetPort(oldPort);
  265. }
  266. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  267. static    void    ThisWindowActivate    (EventRecord* theEvent, WindowPtr theWindow)
  268. {
  269. OurWinHandle    theData;
  270. GrafPtr            oldPort;
  271.  
  272.  
  273.     GetPort(&oldPort);
  274.     SetPort(theWindow);
  275.  
  276.     theData = (OurWinHandle) GetWRefCon(theWindow);
  277.     
  278.     // Activate items here
  279.     //•••••••••••••••••••••••••••
  280.     
  281.     SetPort(oldPort);
  282. }
  283. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  284. static    void    ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow)
  285. {
  286. OurWinHandle    theData;
  287. GrafPtr            oldPort;
  288.  
  289.  
  290.     GetPort(&oldPort);
  291.     SetPort(theWindow);
  292.  
  293.     theData = (OurWinHandle) GetWRefCon(theWindow);
  294.     
  295.     // Deactivate items here
  296.     //•••••••••••••••••••••••••••
  297.  
  298.     SetPort(oldPort);
  299. }
  300. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  301. static    void    ThisWindowDrag        (EventRecord* theEvent, WindowPtr theWindow)
  302. {    // The default does the right thing
  303. }
  304. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  305. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow)
  306. {
  307. OurWinHandle    theData;
  308. GrafPtr            oldPort;
  309. DialogPtr        theDialog;
  310. short            theItem;
  311.  
  312.  
  313.     GetPort(&oldPort);
  314.     SetPort(theWindow);
  315.  
  316.     theData = (OurWinHandle) GetWRefCon(theWindow);
  317.     
  318.     DialogSelect( theEvent, &theDialog, &theItem);
  319.  
  320.     // do your idle tasks here
  321.     //•••••••••••••••••••••••••••    
  322.  
  323.     SetPort(oldPort);
  324. }
  325. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  326. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow)
  327. {
  328. OurWinHandle    theData;
  329. GrafPtr            oldPort;
  330.  
  331.  
  332.     GetPort(&oldPort);
  333.     SetPort(theWindow);
  334.  
  335.     theData = (OurWinHandle) GetWRefCon(theWindow);
  336.     
  337.     // Adjust the cursor here
  338.     if (PtInRgn( theEvent->where, ((WindowPeek)theWindow)->strucRgn))  {
  339.         // We are in our window
  340.         
  341.         // More complex comparisons should be happening here
  342.         CopyRgn( ((WindowPeek)theWindow)->strucRgn,gMouseMovedRgn);
  343.         
  344.         // You would want to set it to a different cursor
  345.         SetCursor(&arrow);
  346.         }
  347.     else {    // We are outside our window
  348.     
  349.         // Lets make the rgn the whole qd coord
  350.         SetRectRgn(gMouseMovedRgn, -32768, -32768, 32767, 32767);
  351.         // except for our window
  352.         DiffRgn(gMouseMovedRgn,((WindowPeek)theWindow)->strucRgn,gMouseMovedRgn);
  353.         // and set it to an arrow
  354.         SetCursor(&arrow);
  355.         }
  356.     
  357.     SetPort(oldPort);
  358. }
  359. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  360. static    void    ThisWindowKeyDown    (EventRecord* theEvent, WindowPtr theWindow)
  361. {
  362. OurWinHandle    theData;
  363. GrafPtr            oldPort;
  364. short            keyCode;
  365. short            charCode;
  366. Point            localPoint;
  367. DialogPtr        theDialog;
  368. short            theItem;
  369.  
  370.     GetPort(&oldPort);
  371.     SetPort(theWindow);
  372.  
  373.     theData = (OurWinHandle) GetWRefCon(theWindow);
  374.  
  375.     localPoint = theEvent->where;
  376.     GlobalToLocal(&localPoint);
  377.     
  378.     keyCode  = (theEvent->message&keyCodeMask)>>8;
  379.     charCode = theEvent->message&charCodeMask;
  380.  
  381.     DialogSelect( theEvent, &theDialog, &theItem);
  382.  
  383.     // Handle the key click here
  384.     
  385.     
  386.     SetPort(oldPort);
  387. }
  388. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  389. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow)
  390. {
  391. OurWinHandle    theData;
  392. GrafPtr            oldPort;
  393.  
  394.  
  395.     GetPort(&oldPort);
  396.     SetPort(theWindow);
  397.  
  398.     theData = (OurWinHandle) GetWRefCon(theWindow);
  399.     
  400.     // Enable and disable items
  401.     // change item names etc.    
  402.     
  403.     SetPort(oldPort);
  404. }
  405. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  406. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow)
  407. {
  408. OurWinHandle    theData;
  409. GrafPtr            oldPort;
  410.  
  411.  
  412.     GetPort(&oldPort);
  413.     SetPort(theWindow);
  414.  
  415.     theData = (OurWinHandle) GetWRefCon(theWindow);
  416.     
  417.     // Enable and disable items
  418.     // change item names etc.
  419.     
  420.     
  421.     SetPort(oldPort);
  422. }
  423. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  424. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID)
  425. {
  426. short    theResult;
  427.  
  428.     theResult = TRUE;
  429.     
  430.     // if we don't handle it return false so the defaults will pick it up
  431.     switch (theMenu) {
  432.         default:
  433.             theResult = FALSE;
  434.         }
  435.         
  436. return theResult;
  437. }
  438. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  439. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect)
  440. {
  441. // This simply is the size of the rect the window can grow to
  442. SetRect(theRect, 64, 64, 32767, 32767);
  443. }
  444. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  445. static    void    ThisWindowBackground(EventRecord* theEvent, WindowPtr theWindow)
  446. {
  447. // This routine will get called while the window is in the background
  448. // you may want a window to complete some task while not in the forground, etc.
  449. // NOTE:     If you do not do anything in this routine you can set the 
  450. //            windowBackground field to NULL. That will avoid a call
  451. //            THIS IS THE ONLY function that you can do that with!!
  452. }
  453. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  454. static    void    ThisWindowGetScrap(EventRecord* theEvent, WindowPtr theWindow)
  455. {
  456. // This routine will get called when your application gets a resumeEvent 
  457. // AND the convertClipboardFlag bit is set
  458. // What must be done is the convertion of the clipboard to a private scrap
  459. TEFromScrap();
  460. }
  461. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  462. static    void    ThisWindowPutScrap(EventRecord* theEvent, WindowPtr theWindow)
  463. {
  464. // This routine will get called when your application gets a suspendEvent 
  465. // What must be done is the convertion of the private scrap to the clipboard
  466. TEToScrap();
  467. }
  468. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••